package com.bluefish.bltuan.action.email;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.action.OpenAction;
import com.bluefish.bltuan.module.Category;
import com.bluefish.bltuan.service.CategoryService;
import com.bluefish.bltuan.service.SubscriberService;
import com.bluefish.bltuan.tools.FormatUtils;

public class SubscribeAction extends OpenAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2361925505677051591L;

	private SubscriberService subscriberService;
	
	private CategoryService categoryService;
	
	private String email;
	
	private Integer cityId;

	private boolean success;
	
	@Override
	public HttpHeaders create() {
		String tip = getText("subscribeSuccess");
		if(this.email != null && FormatUtils.isEmail(email)){
			Category city = this.categoryService.findById(this.cityId);
			this.success = this.subscriberService.subscribe(this.email ,city);
			if(!this.success)
				tip = getText("subscribeEmailUserd");
		}else{
			tip = getText("emailFormatError");
		}
		addActionMessage(tip);
		super.index();
		return new DefaultHttpHeaders(TIP).disableCaching();
	}

	@Override
	public String destroy() {
		return null;
	}

	@Override
	public String edit() {
		return null;
	}

	@Override
	public HttpHeaders editNew() {
		return null;
	}

	@Override
	public HttpHeaders index() {
		super.index();
		return new DefaultHttpHeaders(INDEX).disableCaching();
	}

	@Override
	public HttpHeaders show() {
		return null;
	}

	@Override
	public String update() {
		return null;
	}

	public void setSubscriberService(SubscriberService subscriberService) {
		this.subscriberService = subscriberService;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public void setCityId(Integer cityId) {
		this.cityId = cityId;
	}
	
	public boolean isSuccess() {
		return success;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
}
